using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.ServiceObjects;

namespace OracleInternal.ConnectionPool
{
	internal class CriteriaMapper
	{
		internal enum ID_TYPE
		{
			ConnectionId = 1,
			EditionId,
			TagId
		}

		private SyncDictionary<string, uint> m_dictConnectionId;

		private SyncDictionary<string, uint> m_dictEditionId;

		private uint m_ConnectionIdCounter = 1u;

		private uint m_EditionIdCounter = 1u;

		internal object m_syncCriteriaCtx;

		internal object m_syncConnCls;

		internal object m_syncEdition;

		internal CriteriaMapper()
		{
			m_dictConnectionId = new SyncDictionary<string, uint>();
			m_dictEditionId = new SyncDictionary<string, uint>();
			m_syncCriteriaCtx = new object();
			m_syncConnCls = new object();
			m_syncEdition = new object();
		}

		internal void GetId(OracleConnectionImpl pr)
		{
			if (pr == null)
			{
				return;
			}
			lock (m_syncCriteriaCtx)
			{
				if (pr != null)
				{
					if (!string.IsNullOrEmpty(pr.m_connectionClass))
					{
						pr.m_criteriaIds[0] = m_dictConnectionId[pr.m_connectionClass];
					}
					else
					{
						pr.m_criteriaIds[0] = 0u;
					}
					if (!string.IsNullOrEmpty(pr.EditionName))
					{
						pr.m_criteriaIds[1] = m_dictEditionId[pr.EditionName];
					}
					else
					{
						pr.m_criteriaIds[1] = 0u;
					}
				}
			}
		}

		internal void AssignId(OracleConnection connRefForCriteria)
		{
			if (connRefForCriteria == null)
			{
				return;
			}
			lock (m_syncCriteriaCtx)
			{
				if (connRefForCriteria == null)
				{
					return;
				}
				if (!string.IsNullOrEmpty(connRefForCriteria.m_criteriaConnCls))
				{
					if (m_dictConnectionId.ContainsKey(connRefForCriteria.m_criteriaConnCls))
					{
						connRefForCriteria.m_criteriaIds[0] = m_dictConnectionId[connRefForCriteria.m_criteriaConnCls];
					}
					else
					{
						AddId(connRefForCriteria.m_criteriaConnCls, 1);
						connRefForCriteria.m_criteriaIds[0] = m_dictConnectionId[connRefForCriteria.m_criteriaConnCls];
					}
				}
				else
				{
					connRefForCriteria.m_criteriaIds[0] = 0u;
				}
				if (!string.IsNullOrEmpty(connRefForCriteria.m_criteriaEdition))
				{
					if (m_dictEditionId.ContainsKey(connRefForCriteria.m_criteriaEdition))
					{
						connRefForCriteria.m_criteriaIds[1] = m_dictEditionId[connRefForCriteria.m_criteriaEdition];
						return;
					}
					AddId(connRefForCriteria.m_criteriaEdition, 2);
					connRefForCriteria.m_criteriaIds[1] = m_dictEditionId[connRefForCriteria.m_criteriaEdition];
				}
				else
				{
					connRefForCriteria.m_criteriaIds[1] = 0u;
				}
			}
		}

		internal void AddId(string id_value, int id_type)
		{
			if (id_value == null)
			{
				return;
			}
			switch (id_type)
			{
			case 1:
				lock (m_syncConnCls)
				{
					if (!m_dictConnectionId.ContainsKey(id_value))
					{
						m_dictConnectionId[id_value] = m_ConnectionIdCounter++;
					}
				}
				break;
			case 2:
				lock (m_syncEdition)
				{
					if (!m_dictEditionId.ContainsKey(id_value))
					{
						m_dictEditionId[id_value] = m_EditionIdCounter++;
					}
				}
				break;
			}
		}

		internal void RemoveId(string id_value, int id_type)
		{
			if (id_value != null)
			{
				switch (id_type)
				{
				case 1:
					m_dictConnectionId.Remove(id_value);
					break;
				case 2:
					m_dictEditionId.Remove(id_value);
					break;
				}
			}
		}
	}
}
